package com.ruoyi.project.module.gs;


import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;

import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.CxcyContract;
import com.ruoyi.project.domain.CxcyExpense;
import com.ruoyi.project.domain.ISysUser;
import com.ruoyi.project.module.cjl.CjlWeChatAuditMassage;
import com.ruoyi.project.module.cjl.CjlWeChatMassage;
import com.ruoyi.project.service.ICxcyContractService;
import com.ruoyi.project.service.ICxcyExpenseService;

import com.ruoyi.project.service.ISysUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;

/**
 * 合同信息Controller
 * 
 * @author ruoyi
 * @date 2023-05-10
 */
@RestController
@RequestMapping("/gscontract")
public class GsCxcyContractController extends BaseController
{
    @Autowired
    private ICxcyContractService cxcyContractService;

    @Autowired
    private ICxcyExpenseService cxcyExpenseService;
    @Autowired
    private ISysUserService userService;
    /**
     * 查询合同信息列表
     */
    @RequiresPermissions("project:gscontract:lista")
    @GetMapping("/list1")
    public TableDataInfo list1(CxcyContract cxcyContract)
    {
        startPage();
        List<CxcyContract> list = cxcyContractService.selectCxcyContractList(cxcyContract);
        List<CxcyContract> newList = new ArrayList<>();
        for (CxcyContract contract : list) {
            CxcyExpense expense = new CxcyExpense();
            expense.setResultId(contract.getResultId());
            List<CxcyExpense> expenses = cxcyExpenseService.selectCxcyExpenseList(expense);
            if (expenses.size() > 0 && !"1".equals(expenses.get(0).getIsReceive())) {
                continue;
            }
            newList.add(contract);
        }
        return getDataTable(newList);
    }
    @RequiresPermissions("project:gscontract:listb")
    @GetMapping("/list")
    public TableDataInfo list(CxcyContract cxcyContract)
    {
        startPage();


        List<CxcyContract> list = cxcyContractService.selectCxcyContractList(cxcyContract);




        return getDataTable(list);
    }
    /**
     * 导出合同信息列表
     */
    @RequiresPermissions("project:gscontract:export")
    @Log(title = "合同信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CxcyContract cxcyContract)
    {
        List<CxcyContract> list = cxcyContractService.selectCxcyContractList(cxcyContract);
        ExcelUtil<CxcyContract> util = new ExcelUtil<CxcyContract>(CxcyContract.class);
        util.exportExcel(response, list, "合同信息数据");
    }

    /**
     * 获取合同信息详细信息
     */
    @RequiresPermissions("project:gscontract:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cxcyContractService.selectCxcyContractById(id));
    }

    /**
     * 新增合同信息
     */
    @RequiresPermissions("project:gscontract:add")
    @Log(title = "合同信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CxcyContract cxcyContract)
    {
        return toAjax(cxcyContractService.insertCxcyContract(cxcyContract));
    }

    /**
     * 修改合同信息
     */
    @RequiresPermissions("project:gscontract:edit")
    @Log(title = "合同信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CxcyContract cxcyContract)
    {

        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        cxcyContract.setPublicizeName(sysUser.getNickName());
        cxcyContract.setPublicizeNumber(sysUser.getUserName());

        CxcyExpense cx=new CxcyExpense();
        cx.setResultId(cxcyContract.getResultId());
        List<CxcyExpense> list = cxcyExpenseService.selectCxcyExpenseList(cx);

        cxcyContract.setNote2(list.get(0).getRemittingPrice());


        return toAjax(cxcyContractService.updateCxcyContract(cxcyContract));
    }


    @RequiresPermissions("project:gscontract:yuannedit")
    @Log(title = "合同信息", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/yuannedit")
    public AjaxResult yuannedit(@RequestBody CxcyContract cxcyContract)
    {


        return toAjax(cxcyContractService.updateCxcyContract(cxcyContract));
    }


    /**
     * 提交初审
     */
    @RequiresPermissions("project:gscontract:chuedit")
    @Log(title = "合同信息", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/chushen")
    public AjaxResult chuedit(@RequestBody CxcyContract cxcyContract)
    {
        cxcyContract.setAuditState1("1");
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        cxcyContract.setApplyName1(sysUser.getNickName());
        cxcyContract.setApplyNumber1(sysUser.getUserName());
// 获取当前时间
        Date now = new Date();
        cxcyContract.setApplyTime1(now);
        ISysUser User = new ISysUser();
        User.setRemark("teacher");
        List<ISysUser> list2 = userService.selectMsgUserList(User);
        for (ISysUser user:list2) {
            GsWeChatMassage.sendWeChatMassage(cxcyContract, user);
        }
        return toAjax(cxcyContractService.updateCxcyContract(cxcyContract));
    }




    /**
     * 进行初审
     */
    @RequiresPermissions("project:gscontract:jinxingchuedit")
    @Log(title = "合同信息", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/jinxingchushen")
    public AjaxResult jinxingchuedit(@RequestBody CxcyContract cxcyContract) throws IOException {


// 获取当前时间
        Date now = new Date();
        cxcyContract.setAuditTime1(now);
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        cxcyContract.setAuditName1(sysUser.getNickName());
        cxcyContract.setAuditNumber1(sysUser.getUserName());


        if(cxcyContract.getAuditState1().equals("2")) {
            CxcyExpense cxcyExpense = new CxcyExpense();
            cxcyExpense.setResultId(cxcyContract.getResultId());
            cxcyExpense.setResultName(cxcyContract.getResultName());
            cxcyExpense.setFirstName(cxcyContract.getFirstName());
            cxcyExpense.setFirstNumber(cxcyContract.getFirstNumber());
            cxcyExpense.setFirstPhone(cxcyContract.getFirstPhone());
            cxcyExpense.setFirstIdnumber(cxcyContract.getFirstIdnumber());
            cxcyExpense.setEnterpriseNumber(cxcyContract.getEnterpriseNumber());
            cxcyExpense.setEnterpriseName(cxcyContract.getEnterpriseName());
            cxcyExpense.setCorporateName(cxcyContract.getCorporateName());
            cxcyExpense.setCorporatePhone(cxcyContract.getCorporatePhone());
            cxcyExpense.setCorporateNumber(cxcyContract.getCorporateNumber());
            cxcyExpense.setUserId(cxcyContract.getUserId());
            cxcyExpenseService.insertCxcyExpense(cxcyExpense);
        }

        GsWeChatAuditMassage.sendWeChatAuditMassage(cxcyContract);
        return toAjax(cxcyContractService.updateCxcyContract(cxcyContract));
    }


    /**
     * 进行二审
     */
    @RequiresPermissions("project:gscontract:jinxingeredit")
    @Log(title = "合同信息", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/jinxingershen")
    public AjaxResult jinxingeredit(@RequestBody CxcyContract cxcyContract)
    {
        CxcyExpense cxcyExpense=new CxcyExpense();
        cxcyExpense.setResultId(cxcyContract.getResultId());
        cxcyExpense.setResultName(cxcyContract.getResultName());
        cxcyExpense.setFirstName(cxcyContract.getFirstName());
        cxcyExpense.setFirstNumber(cxcyContract.getFirstNumber());
        cxcyExpense.setFirstPhone(cxcyContract.getFirstPhone());
        cxcyExpense.setFirstIdnumber(cxcyContract.getFirstIdnumber());
        cxcyExpense.setEnterpriseNumber(cxcyContract.getEnterpriseNumber());
        cxcyExpense.setEnterpriseName(cxcyContract.getEnterpriseName());
        cxcyExpense.setCorporateName(cxcyContract.getCorporateName());
        cxcyExpense.setCorporatePhone(cxcyContract.getCorporatePhone());
        cxcyExpense.setCorporateNumber(cxcyContract.getCorporateNumber());
        cxcyExpenseService.insertCxcyExpense(cxcyExpense);


        return toAjax(cxcyContractService.updateCxcyContract(cxcyContract));
    }






    /**
     * 删除合同信息
     */
    @RequiresPermissions("project:gscontract:remove")
    @Log(title = "合同信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cxcyContractService.deleteCxcyContractByIds(ids));
    }
}
